from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _


class Exercise(models.Model):
    """
    This model represents the exercise book.
    One Course can have many exercise book.
    One exercise book will contains many questions.
    """

    name = models.CharField(max_length=64)
    created_time = models.DateTimeField(auto_now_add=True)
    updated_time = models.DateTimeField(auto_now=True)

    course = models.ForeignKey(
        'course.Course', on_delete=models.CASCADE, related_name='exercises')

    class Meta:
        db_table = 'exercise'

    def __str__(self) -> str:
        return f'<Exercise No.{self.id} for {self.course}>'


class Question(models.Model):

    class QUETION_CATEGORY(models.IntegerChoices):
        SHORT_ANSWER = 0, _("问答题")
        SINGLE_SELECT = 1, _("单选题")

    category = models.PositiveIntegerField(
        choices=QUETION_CATEGORY.choices, default=QUETION_CATEGORY.SHORT_ANSWER, null=False, blank=False)
    content = models.TextField(blank=False, null=False)
    score = models.PositiveSmallIntegerField(default=5)
    answer = models.TextField(blank=False, null=False)
    created_time = models.DateTimeField(auto_now_add=True)

    exercise = models.ForeignKey(
        'exercise.Exercise', on_delete=models.CASCADE, related_name='questions')

    class Meta:
        db_table = 'question'

    def __str__(self) -> str:
        return f'<Question No.{self.id} for {self.exercise}>'


class Answer(models.Model):
    """
    Answer submitted by users.
    """
    content = models.TextField(blank=False, null=False)
    created_time = models.DateTimeField(auto_now_add=True)
    submitter = models.ForeignKey(
        'user.User', on_delete=models.CASCADE, related_name='my_answers')
    question = models.ForeignKey(
        'exercise.Question', on_delete=models.CASCADE, related_name='submit_answers')

    class Meta:
        db_table = 'answer'

    def __str__(self) -> str:
        return f'<Answer for {self.question}>'
